<?php 

/**
* 上传类
*/
include "../function.php";
class Upload
{
	public $config=array(
		'path'=>'./Upload/',
		'max_size'=>150000,
		'type'=>['jpg','png','gif']
		);
	public $path;

	public $error;
	function __construct($config=array())
	{	
		if (!empty($config)) {
			$this->config=$config;
		}
	}
	public function save()
	//总方法
	{	
		$this->path=current($_FILES);
		// var_dump($this->path);
		// exit;
		$this->cheError();
		$this->checksize();
		$this->checktype();
		$this->move();


		// var_dump($this->path);
	}
	private function cheError()
	{
		foreach ($this->path['name'] as $k => $v) {
			if ($this->path['error'][$k]!=0) {
				$this->error= $this->path['name'][$k]."上传出错，错误是:".$this->path['error'][$k];
				unset($this->path['name'][$k]);
			}
		}
	}
	private function checksize()
	//在这个类里面的一个私有方法
	{
		// var_dump($this->path);
		foreach ($this->path['name'] as $k => $v) {
			if ($this->path["size"][$k]>$this->config['max_size']) {
				$this->error= $this->path['name'][$k]."文件大过".$this->config['max_size'];
				unset($this->path['name'][$k]);
			//删除这个过大的	
			}
		}
	}
	private function checktype()
	{
		foreach ($this->path['name'] as $k => $v) {
			$arr=explode('.', $this->path['name'][$k]);
			$suffix=end($arr);
			$this->path['suffix'][$k]=$suffix;

			var_dump($this->path['suffix'][$k]);

				if (!in_array($suffix, $this->config['type'])) {
					$this->error= $this->path['name'][$k]."文件格式不对";
					
					unset($this->path['name'][$k]);
				}		
		}

	}
	private function move()
	{	
		foreach ($this->path['name'] as $k => $v) {
			$name=uniqid();
			is_dir($this->config['path'])||mkdir($this->config['path']);

			move_uploaded_file($this->path['tmp_name'][$k],$this->config['path'].$name.'.'.$this->path['suffix'][$k]);


		}	
	}
}





 ?>